package com.example.tys.baseapplication.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

/**
 * Created by tys on 2019/2/17.
 */
@CoordinatorLayout.DefaultBehavior(MyView.Behavior.class)
public class MyView extends android.support.v7.widget.AppCompatTextView {
    private Paint paint;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
    }


    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(getLeft(), getTop(), 30, paint);
    }

    public static class Behavior extends CoordinatorLayout.Behavior<MyView> {

        public Behavior() {
        }

        public Behavior(Context context, AttributeSet attrs) {
            super(context, attrs);
        }


        @Override
        public boolean layoutDependsOn(CoordinatorLayout parent, MyView child, View dependency) {
            Log.e("0-0-0-", "layoutDependsOn");
//            return super.layoutDependsOn(parent, child, dependency);
            return dependency instanceof Button;
        }

        @Override
        public boolean onDependentViewChanged(CoordinatorLayout parent, MyView child, View dependency) {
            Log.e("0-0-0-", "onDependentViewChanged");
            child.setX(dependency.getX() + 120);
            child.setY(dependency.getY() + 120);
//            child.setText(dependency.getX()+","+dependency.getY());
            return true;
        }

        @Override
        public void onDependentViewRemoved(CoordinatorLayout parent, MyView child, View dependency) {
            super.onDependentViewRemoved(parent, child, dependency);
            Log.e("0-0-0-", "onDependentViewRemoved");
        }

        @Override
        public void onAttachedToLayoutParams(@NonNull CoordinatorLayout.LayoutParams params) {
            super.onAttachedToLayoutParams(params);
            Log.e("0-0-0-", "onAttachedToLayoutParams");
        }

        @Override
        public void onDetachedFromLayoutParams() {
            super.onDetachedFromLayoutParams();
            Log.e("0-0-0-", "onDetachedFromLayoutParams");
        }

        @Override
        public boolean onInterceptTouchEvent(CoordinatorLayout parent, MyView child, MotionEvent ev) {
            Log.e("0-0-0-", "onInterceptTouchEvent:" + ev.getAction());
            return super.onInterceptTouchEvent(parent, child, ev);
        }

        @Override
        public boolean onTouchEvent(CoordinatorLayout parent, MyView child, MotionEvent ev) {
            Log.e("0-0-0-", "onTouchEvent:" + ev.getAction());
            return super.onTouchEvent(parent, child, ev);
        }

        @Override
        public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull MyView child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
            Log.e("0-0-0-", "onStartNestedScroll");
            return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type);
        }

        @Override
        public void onNestedScrollAccepted(@NonNull CoordinatorLayout coordinatorLayout, @NonNull MyView child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
            Log.e("0-0-0-", "onNestedScrollAccepted");
            super.onNestedScrollAccepted(coordinatorLayout, child, directTargetChild, target, axes, type);
        }

        @Override
        public void onStopNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull MyView child, @NonNull View target, int type) {
            Log.e("0-0-0-", "onStopNestedScroll");
            super.onStopNestedScroll(coordinatorLayout, child, target, type);
        }

        @Override
        public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull MyView child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
            Log.e("0-0-0-", "onNestedScroll");
            super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
        }

        @Override
        public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull MyView child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
            super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
        }

        @Override
        public boolean onNestedFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull MyView child, @NonNull View target, float velocityX, float velocityY, boolean consumed) {
            return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
        }

        @Override
        public boolean onNestedPreFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull MyView child, @NonNull View target, float velocityX, float velocityY) {
            return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);

        }


    }
}
